﻿
'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:'
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System
Imports System.ComponentModel

Imports System.Collections.Generic
Imports System.Collections.ObjectModel


Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Collections
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Visio


Imports visRackComponentAttributes.clsVisioSupport
Imports visRackComponentAttributes.clsDC_RackData
Imports visRackComponentAttributes.dc_Component

Public Class clsAttrVirtualHost

#Region " properties and definitions "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_VirtualHost
    Public m_oc As Collection(Of dc_VirtualHost)

    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand


    Private _clsVisio As visRackComponentAttributes.clsVisioSupport

    Public Const readAll As String = "selectTblAttrVirtualHost"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblAttrVirtualHost

    Private m_visshape As Microsoft.Office.Interop.Visio.Shape
    Public Property _VisShape() As Microsoft.Office.Interop.Visio.Shape
        Get
            Return Me.m_visshape
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Shape)
            m_visshape = value
        End Set
    End Property

    '********* visio snippet vb.net vis0041vb ******************
    Private _drawingApplication As Microsoft.Office.Interop.Visio.Application
    Public Property drawingApplication() As Microsoft.Office.Interop.Visio.Application
        Get
            Return Me._drawingApplication
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Application)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("ApplicationValueIsNull", _
                    "drawingApplication is Null.")
            End If
            Me._drawingApplication = value
        End Set
    End Property

    '********* visio snippet vb.net vis0043vb ******************
    Private _drawingDocument As Microsoft.Office.Interop.Visio.Document
    Public Property drawingDocument() As Microsoft.Office.Interop.Visio.Document
        Get
            Return Me._drawingDocument
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Document)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("DocumentValueIsNull", _
                    "drawingDocument is Null.")
            End If
            Me._drawingDocument = value
        End Set
    End Property

    '********* visio snippet vb.net vis0045vb ******************
    Private _drawingWindow As Microsoft.Office.Interop.Visio.Window
    Public Property drawingWindow() As Microsoft.Office.Interop.Visio.Window
        Get
            Return Me._drawingWindow
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Window)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("WindowValueIsNull", _
                    "drawingWindow is Null.")
            End If
            Me._drawingWindow = value
        End Set
    End Property

    '********* visio snippet vb.net vis0044vb ******************
    Private _drawingPage As Microsoft.Office.Interop.Visio.Page
    Public Property drawingPage() As Microsoft.Office.Interop.Visio.Page
        Get
            Return Me._drawingPage
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Page)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("PageValueIsNull", _
                    "drawingPage is Null.")
            End If
            Me._drawingPage = value
        End Set
    End Property

    Public m_customstencilpath As String = My.Settings.path_custom_stencils
    Public Property _CustomStencilPath() As String
        Get
            Return Me.m_customstencilpath
        End Get
        Set(ByVal value As String)
            Me.m_customstencilpath = value
        End Set
    End Property



#End Region

#Region " Move/Transform Data "


    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_VirtualHost = New dc_VirtualHost(strUserId)

        Try

            Dim colvhKey As New System.Data.DataColumn
            colvhKey.ColumnName = "vhKey"
            colvhKey.DataType = GetType(Int32)
            colvhKey.DefaultValue = m_dc.vhKey
            tempTable.Columns.Add(colvhKey)

            Dim colvhOSKey As New System.Data.DataColumn
            colvhOSKey.ColumnName = "vhOSKey"
            colvhOSKey.DataType = GetType(Int32)
            colvhOSKey.DefaultValue = m_dc.vhOSKey
            tempTable.Columns.Add(colvhOSKey)

            Dim colvhName As New System.Data.DataColumn
            colvhName.ColumnName = "vhName"
            colvhName.DataType = GetType(String)
            colvhName.DefaultValue = m_dc.vhName
            tempTable.Columns.Add(colvhName)

            Dim colvhVendor As New System.Data.DataColumn
            colvhVendor.ColumnName = "vhVendor"
            colvhVendor.DataType = GetType(String)
            colvhVendor.DefaultValue = m_dc.vhVendor
            tempTable.Columns.Add(colvhVendor)

            Dim colvhVersion As New System.Data.DataColumn
            colvhVersion.ColumnName = "vhVersion"
            colvhVersion.DataType = GetType(String)
            colvhVersion.DefaultValue = m_dc.vhVersion
            tempTable.Columns.Add(colvhVersion)

            Dim colvhRelease As New System.Data.DataColumn
            colvhRelease.ColumnName = "vhRelease"
            colvhRelease.DataType = GetType(String)
            colvhRelease.DefaultValue = m_dc.vhRelease
            tempTable.Columns.Add(colvhRelease)

            Dim colvhServicePack As New System.Data.DataColumn
            colvhServicePack.ColumnName = "vhServicePack"
            colvhServicePack.DataType = GetType(String)
            colvhServicePack.DefaultValue = m_dc.vhServicePack
            tempTable.Columns.Add(colvhServicePack)

            Dim colvhFixLevel As New System.Data.DataColumn
            colvhFixLevel.ColumnName = "vhFixLevel"
            colvhFixLevel.DataType = GetType(String)
            colvhFixLevel.DefaultValue = m_dc.vhFixLevel
            tempTable.Columns.Add(colvhFixLevel)

            Dim colvhFileName As New System.Data.DataColumn
            colvhFileName.ColumnName = "vhFilename"
            colvhFileName.DataType = GetType(String)
            colvhFileName.DefaultValue = m_dc.vhFileName
            tempTable.Columns.Add(colvhFileName)

            Dim colvhCreationDate As New System.Data.DataColumn
            colvhCreationDate.ColumnName = "vhCreationDate"
            colvhCreationDate.DataType = GetType(Date)
            colvhCreationDate.DefaultValue = m_dc.vhCreationDate
            tempTable.Columns.Add(colvhCreationDate)

            Dim colvhComments As New System.Data.DataColumn
            colvhComments.ColumnName = "vhComments"
            colvhComments.DataType = GetType(String)
            colvhComments.DefaultValue = m_dc.vhComments
            tempTable.Columns.Add(colvhComments)

            Dim colvhLastEdit As New System.Data.DataColumn
            colvhLastEdit.ColumnName = "vhLastEdit"
            colvhLastEdit.DataType = GetType(String)
            colvhLastEdit.DefaultValue = m_dc.vhLastEdit
            tempTable.Columns.Add(colvhLastEdit)

            Dim colvhLastUpdate As New System.Data.DataColumn
            colvhLastUpdate.ColumnName = "vhLastUpdate"
            colvhLastUpdate.DataType = GetType(Date)
            colvhLastUpdate.DefaultValue = m_dc.vhLastUpdate
            tempTable.Columns.Add(colvhLastUpdate)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If


    End Sub


    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
                (ByRef tempRow As DataRow, _
                 ByVal tempDC As dc_VirtualHost)

        Try

            With tempDC
                tempRow.SetField("vhKey", .vhKey)
                tempRow.SetField("vhOSKey", .vhOSKey)
                tempRow.SetField("vhName", .vhName)
                tempRow.SetField("vhVendor", .vhVendor)
                tempRow.SetField("vhVersion", .vhVersion)
                tempRow.SetField("vhRelease", .vhRelease)
                tempRow.SetField("vhServicePack", .vhServicePack)
                tempRow.SetField("vhFixLevel", .vhFixLevel)
                tempRow.SetField("vhFileName", .vhFileName)
                tempRow.SetField("vhCreationDate", .vhCreationDate)
                tempRow.SetField("vhComments", .vhComments)
                tempRow.SetField("vhLastEdit", strUserId)
                tempRow.SetField("vhLastUpdate", .vhLastUpdate)
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByRef tempRow As DataRow)

        Try

            With m_dc
                .vhKey = tempRow.Item("vhKey")
                .vhOSKey = tempRow.Item("vhOSKey")
                .vhName = tempRow.Item("vhName")
                .vhVendor = tempRow.Item("vhVendor")
                .vhVersion = tempRow.Item("vhVersion")
                .vhRelease = tempRow.Item("vhRelease")
                .vhServicePack = tempRow.Item("vhServicePack")
                .vhFixLevel = tempRow.Item("vhFixLevel")
                .vhFileName = tempRow.Item("vhFileName")
                .vhCreationDate = tempRow.Item("vhCreationDate")
                .vhComments = tempRow.Item("vhComments")
                .vhLastEdit = tempRow.Item("vhLastEdit")
                .vhLastUpdate = tempRow.Item("vhLastUpdate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
            (ByVal tempRow As DataRow, _
             ByRef tempDC As dc_VirtualHost)

        Try

            With tempDC
                .vhKey = tempRow.Item("vhKey")
                .vhOSKey = tempRow.Item("vhOSKey")
                .vhName = tempRow.Item("vhName")
                .vhVendor = tempRow.Item("vhVendor")
                .vhVersion = tempRow.Item("vhVersion")
                .vhRelease = tempRow.Item("vhRelease")
                .vhServicePack = tempRow.Item("vhServicePack")
                .vhFixLevel = tempRow.Item("vhFixLevel")
                .vhFileName = tempRow.Item("vhFileName")
                .vhCreationDate = tempRow.Item("vhCreationDate")
                .vhComments = tempRow.Item("vhComments")
                .vhLastEdit = tempRow.Item("vhLastEdit")
                .vhLastUpdate = tempRow.Item("vhLastUpdate")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
            (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_VirtualHost)
                Else
                    m_oc = New Collection(Of dc_VirtualHost)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_VirtualHost
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_VirtualHost
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function


#End Region

#Region " standard database "



    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_VirtualHost)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_VirtualHost)
            End If

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_VirtualHost(strUserId)
            Else
                m_dc = New dc_VirtualHost(strUserId)
            End If

            CreateTableColumns(m_dt)
            m_dt.TableName = dc_racktables.tblattrvirtualhost.tostring
            m_ds.Tables.Add(m_dt)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addAttrVirtualHostDataSourceRecord _
        (ByVal tempDC As dc_VirtualHost, _
         Optional ByVal blnSilent As Boolean = False) _
        As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(dc_racktables.tblattrvirtualhost.tostring).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(dc_racktables.tblattrvirtualhost.tostring).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()


            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function


    '
    '
    '
    Private Sub buildInsertVirtualHostCommand()

        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertAttrVirtualHost")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand
            m_db.AddInParameter(cmdActive, "vhOSKey", DbType.Int32, "vhOSKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhName", DbType.String, "vhName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhVendor", DbType.String, "vhVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhVersion", DbType.String, "vhVersion", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhRelease", DbType.String, "vhRelease", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhServicePack", DbType.String, "vhServicePack", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhFixLevel", DbType.String, "vhFixLevel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhFileName", DbType.String, "vhFileName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhCreationDate", DbType.Date, "vhCreationDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhComments", DbType.String, "vhComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhLastEdit", DbType.String, "vhLastEdit", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhLastUpdate", DbType.Date, "vhLastUpdate", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Private Sub buildUpdateVirtualHostCommand()

        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateAttrVirtualHost")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand
            m_db.AddInParameter(cmdActive, "vhKey", DbType.Int32, "vhKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhOSKey", DbType.Int32, "vhOSKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhName", DbType.String, "vhName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhVendor", DbType.String, "vhVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhVersion", DbType.String, "vhVersion", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhRelease", DbType.String, "vhRelease", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhServicePack", DbType.String, "vhServicePack", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhFixLevel", DbType.String, "vhFixLevel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhFileName", DbType.String, "vhFileName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhCreationDate", DbType.Date, "vhCreationDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhComments", DbType.String, "vhComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhLastEdit", DbType.String, "vhLastEdit", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "vhLastUpdate", DbType.Date, "vhLastUpdate", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteVirtualHostCommand()

        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteVirtualHost")

            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand

            m_db.AddInParameter(cmdActive, "vhKey", DbType.Int32, "vhKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblattrvirtualhost.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function



#End Region

#Region " database calls "

    '
    '
    '
    Public Sub dbSelectAllVirtualHosts()


        Try


            initDataSetAndTable()


            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrvirtualhost.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblattrvirtualhost.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    '
    '
    Public Sub dbSelectVirtualHostBySystemKey(ByVal intOSKey As Int32)


        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectVirtualHostBySystemKey")
            m_db.AddInParameter(cmd, "@vhOSKey", DbType.Int32, intOSKey)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblAttrVirtualHost.ToString)

            m_dt = m_ds.Tables(DC_RackTables.tblAttrVirtualHost.ToString)

            If m_dt.Rows.Count = 1 Then
                Dim m_dr As System.Data.DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally
        End Try

    End Sub



    '
    '
    '
    Public Sub dbSelectAttrVirtualHostByRowKey _
        (ByVal lngRowKey As Int32)

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrVirtualHostByRowKey")
            m_db.AddInParameter(cmd, "@RowKey", DbType.Int32, lngRowKey)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblattrvirtualhost.tostring)
            m_dt = m_ds.Tables(dc_racktables.tblattrvirtualhost.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub dbSelectAttrVirtualHostByUrriKeys _
        (ByVal colURRIKeys As Collection(Of clsDC_RackData.rackURRI))

        initDataSetAndTable()


        Dim dtInput As DataTable = New DataTable
        Dim dtOutput As DataTable = New DataTable
        Dim drTemp As DataRow
        Dim lngRackKey As Int32
        Dim colRacks As Collection = New Collection
        Dim rckUrri As clsDC_RackData.rackURRI

        For Each rckURRI In colURRIKeys
            colRacks.Add(rckUrri.rckKey)
        Next rckURRI

        Try

            For intX = 1 To colRacks.Count
                lngRackKey = colRacks(intX)
                If Not (dtInput Is Nothing) Then
                    dtInput = Nothing
                    dtInput = New DataTable
                Else
                    dtInput = New DataTable
                End If
                dtInput = GetAttrVirtualHostByRack(lngRackKey)
                With dtInput
                    If intX = 1 Then
                        dtOutput = Nothing
                        dtOutput = New DataTable
                        dtOutput = dtInput.Copy
                    Else
                        For Each drTemp In .Rows
                            dtOutput.ImportRow(drTemp)
                        Next
                    End If
                End With
            Next intX


            For Each drTemp In dtOutput.Rows
                m_ds.Tables(dc_racktables.tblattrvirtualhost.tostring).ImportRow(drTemp)
            Next

            m_dt = m_ds.Tables(DC_RackTables.tblAttrVirtualHost.ToString)
            m_dt.TableName = DC_RackTables.tblAttrVirtualHost.ToString


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch Ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(Ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function GetAttrVirtualHostByRack _
                (ByVal lngRackKey As Int32) _
                As DataTable

        Dim m_dt1 As DataTable = New DataTable
        Dim dtReturn As DataTable = New DataTable
        Dim m_ds1 As DataSet = New DataSet

        Try

            Dim cmd1 As DbCommand
            cmd1 = m_db.GetStoredProcCommand("selectAttrVirtualHostByRack")
            m_db.AddInParameter(cmd1, "@compKey", DbType.Int32, lngRackKey)
            m_db.LoadDataSet(cmd1, m_ds1, "temp")
            m_dt1 = m_ds1.Tables("temp")

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
            m_dt1 = Nothing
        End Try

        GetAttrVirtualHostByRack = m_dt1

    End Function

    '
    '
    '
    Public Sub dbUpdateVHResetComponentKey(ByVal intCompKey As Int32)


        If intCompKey = -1 Then Exit Sub

        Try

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("updateVHResetComponentKey")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)

            Dim affectedRows As Int16 = m_db.ExecuteNonQuery(cmd)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub



#End Region

#Region " constructor "

    '
    '
    '
    Public Sub New(ByVal visApp As Microsoft.Office.Interop.Visio.Application, _
                   ByVal visDoc As Microsoft.Office.Interop.Visio.Document)


        Try


            m_db = DatabaseFactory.CreateDatabase()

            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            '
            ' init an object class so that the default constants are available
            ' to the table definitions
            '

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_VirtualHost(strUserId)
            Else
                m_dc = New dc_VirtualHost(strUserId)
            End If

            initDataSetAndTable()

            _clsVisio = New visRackComponentAttributes.clsVisioSupport
            _clsVisio.drawingApplication = visApp
            _clsVisio.drawingDocument = visDoc


            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            buildInsertVirtualHostCommand()
            buildDeleteVirtualHostCommand()
            buildUpdateVirtualHostCommand()


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub


#End Region

#Region " Visio Attributes "

    '
    '
    '
    Public Sub addTestVHProperties()

        With m_dc
            .vhKey = -1
            .vhOSKey = -1
            .vhName = "VirtualOne"
            .vhVendor = "Microsoft"
            .vhVersion = "Hyper-V"
            .vhRelease = "1.0"
            .vhServicePack = "sp1"
            .vhFixLevel = "1.0"
        End With


    End Sub

    '
    '
    '
    Public Sub UpdateAttrVirtualHostProperties _
            (ByRef visShape As Microsoft.Office.Interop.Visio.Shape, _
             ByVal drVH As System.Data.DataRow)


        Try
            With m_dc
                If Not (drVH Is Nothing) Then

                    moveDataRowToProperties(drVH)

                Else
                    addTestVHProperties()
                End If

                _clsVisio.ForceShapeData(visShape, "vhKey", .vhKey, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhOSKey", .vhOSKey, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhName", .vhName, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhVendor", .vhVendor, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhVersion", .vhVersion, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhRelease", .vhRelease, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhServicePack", .vhServicePack, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhFixLevel", .vhFixLevel, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhFileName", .vhFileName, "attrVH")
                _clsVisio.ForceShapeData(visShape, "vhCreationDate", .vhCreationDate, "attrVH")

            End With

        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    Private Const constVHPinX As String = "1.5 in"
    Private Const constVHPinY As String = "3.5 in"
    Private Const constVHAlign As VisListAlignment = VisListAlignment.visListAlignLeftOrTop
    Private Const constVHDir As VisListDirection = VisListDirection.visListDirTopToBottom
    Private Const constVHHeader As String = "Virtual Hosts"
    Private Const constVHMaster As String = "Container 4"

    ' wireframe stencil for list (vertical w/header on side and horizontal) toolbar 
    Const stencilWFToolbar As String = "wftlbr_u.vss"
    Const masterDropDown As String = "Drop-down menu"
    Const masterDropDownItem As String = "Menu item"

    '
    '
    '
    Public Function buildSystemVirtualHostContainer _
                        (ByVal intSysKey As Int32, _
                       ByVal _clsData As clsDC_RackData) _
                        As Microsoft.Office.Interop.Visio.Shape

        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing

        ' just to make sure
        _clsVisio.drawingPage = drawingPage
        drawingDocument = drawingPage.Document

        Try



            Dim colInput As Collection = New Collection
            Dim drTemp As System.Data.DataRow = Nothing
            Dim virtualhostQuery = From virtualhost In _clsData.m_ds.Tables(DC_RackTables.tblAttrVirtualHost.ToString).AsEnumerable() _
                       Where virtualhost!vhoskey = intSysKey
                      Select virtualhost

            If 0 < virtualhostQuery.Count Then
                For Each vh In virtualhostQuery
                    colInput.Add("vhKey_" & vh.Item("vhKey").ToString)
                    'System.Diagnostics.Debug.WriteLine(vh.Item("vhName").ToString)
                    'System.Diagnostics.Debug.WriteLine(vh.Item("vhKey").ToString)
                Next vh
            Else
                ' if nothing there then get out
                Return shpContainer
            End If

            ' create a list control
            Dim listControl As Microsoft.Office.Interop.Visio.Shape = Nothing
            ' pass in the created collection and return a populated list control
            Dim colMembers As Collection = New Collection
            colMembers = _clsVisio.dropDropDownBoxItems(colInput, masterDropDownItem, stencilWFToolbar, "1.5 in", ".25 in", vbTrue)

            Dim visShape As Microsoft.Office.Interop.Visio.Shape = Nothing
            Dim visCell As Microsoft.Office.Interop.Visio.Cell = Nothing

            Dim strName As String = ""
            Dim strKey As String = ""

            For Each shapeId In colMembers
                ' get the shape
                visShape = drawingPage.Shapes.ItemFromID(shapeId)
                If visShape.CellExists("user.attrkey", False) = True Then
                    strName = visShape.Cells("user.attrkey").ResultStr("")
                Else
                    strName = ""
                    GoTo NextShape
                End If

                ' go read the current records
                For Each vh In virtualhostQuery
                    strKey = "vhKey_" & vh.Item("vhKey").ToString
                    If strKey = strName Then
                        UpdateAttrVirtualHostProperties(visShape, vh)
                        visShape.Text = vh.Item("vhName").ToString

                        ' move the name to comment field so we get mouse over
                        If visShape.CellsSRCExists(VisSectionIndices.visSectionObject, VisRowIndices.visRowMisc, VisCellIndices.visComment, False) = True Then
                            visCell = visShape.CellsSRC(VisSectionIndices.visSectionObject, VisRowIndices.visRowMisc, VisCellIndices.visComment)
                            _clsVisio.SetCellValueToString(visCell, strName)
                        End If

                        Exit For
                    End If
                Next vh
NextShape:
            Next shapeId

            shpContainer = _clsVisio.addContainerToPage(constVHMaster, "contVirtualHosts", "")

            listControl = _clsVisio.loadListControl(colMembers, constVHAlign, constVHDir)
            listControl.Name = "listVH"
            listControl.Name = "listVH"

            ' add the list control to the container
            _clsVisio.AddShapeToContainer(shpContainer, listControl)

            shpContainer.Cells("pinx").Formula = constVHPinX
            shpContainer.Cells("piny").Formula = constVHPinY



        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
        System.Windows.Forms.Application.DoEvents()
        Return shpContainer

    End Function






#End Region


End Class
